package com.zzu.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zzu.R;
import com.zzu.model.bean.PackageItem;
import com.zzu.utils.ChangeActivity;
import java.util.List;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-13 23:39
 */
public class TwoAdapter extends RecyclerView.Adapter<TwoAdapter.ViewHolder> {

    private static final String TAG = TwoAdapter.class.getName();
    private List<PackageItem> datas = null;
    private Context mContext;

    public TwoAdapter(Context context,List<PackageItem> datas){
        this.datas = datas;
        mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(mContext).inflate(R.layout.item_two, parent, false);
        View view = View.inflate(mContext, R.layout.item_two, null);
//        View view = LayoutInflater.from(mContext).inflate(R.layout.item_two, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        Log.i(TAG,"当前绑定数据的位置"+position+":"+datas.size());
//        if(position >= datas.size())    return ;
        //进行数据绑定
        PackageItem item = datas.get(position);
        //2.设置数据
        holder.tv_two_packageId.setText(item.getId());
        holder.tv_two_sender.setText(item.getSender());
        holder.tv_two_submitTime.setText(item.getUpdateTime());
        holder.tv_two_senderTel.setText(item.getSenderTel());
        holder.tv_two_senderAddress.setText(item.getSenderAddr());
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.i(TAG,"点击的位置"+holder.getLayoutPosition()+"："+position);
//            }
//        });
    }

    @Override
    public int getItemCount() {
//        return Math.max(1,datas.size());
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_two_packageId;
        TextView tv_two_sender;
        TextView tv_two_submitTime;
        TextView tv_two_senderTel;
        TextView tv_two_senderAddress;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_two_packageId = itemView.findViewById(R.id.tv_two_packageId);
            tv_two_sender = itemView.findViewById(R.id.tv_two_sender);
            tv_two_submitTime = itemView.findViewById(R.id.tv_two_submitTime);
            tv_two_senderTel = itemView.findViewById(R.id.tv_two_senderTel);
            tv_two_senderAddress = itemView.findViewById(R.id.tv_two_senderAddress);
            itemView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    //获取位置
                    int position = getLayoutPosition()-1;
                    Log.i(TAG,position+" "+datas.size()+" "+datas);
                    //获取包裹的id
                    String id = datas.get(position).getId();
                    ChangeActivity.ChangeExpressInfo(mContext,id);
                }
            });
        }
    }

//    public List<PackageItem> getDatas() {
//        return datas;
//    }
//
//    public void setDatas(List<PackageItem> datas) {
//        this.datas = datas;
//        notifyDataSetChanged();
//    }

}
